package com.ja.openchat.viewmodels;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.ja.openchat.ui.contact.ContactDetailActivity;
import com.ja.openchat.ui.me.PersonalDataFragment;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.file.FileInfo;
import com.zy.openchat.core.model.file.FileTaskInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;


public class ContactDetailViewModel extends BaseViewModel {

    private ContactDetailActivity contactDetailActivity;

    public ContactDetailViewModel(@NonNull Application application, BaseModel mRepository) {
        super(application);
    }




    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_UpdateContactInfo:
            case EUser_Td_Contact_Photo_Ok: {
                UserInfo updateUser = (UserInfo) tg_event.data;
                if (updateUser != null && updateUser instanceof UserInfo) {
                    if (contactDetailActivity.user != null && contactDetailActivity.user.id == updateUser.id) {
                        contactDetailActivity.user = updateUser;
                        //请求原始联系人信息
                        contactDetailActivity.requestOrgUserInfo();
                        //刷新页面
                        contactDetailActivity.resetUI();
                    }
                }
            }
            break;
            case EUser_Td_Message_Photo_Ok:
            case EUser_Td_Message_Video_Ok:
                if(tg_event.data instanceof FileTaskInfo &&tg_event.dataEx instanceof FileInfo) {
                    FileInfo fileInfo = (FileInfo) tg_event.dataEx;
                    String ids = ((FileTaskInfo) tg_event.data).id;
                    if (!TextUtils.isEmpty(ids) && ids.contains("_")) {
                        String[] idArray = ids.split("_");
                        if (idArray.length > 1) {
                            if(contactDetailActivity!=null){
                                contactDetailActivity.notifyMediaInfo(tg_event.event,fileInfo,idArray[1]);
                            }
                        }
                    }
                }
                break;
        }
    }

    public void setContext(ContactDetailActivity contactDetailActivity) {
        this.contactDetailActivity = contactDetailActivity;
    }

    /**
     * 去个人资料页
     */
    public void goProfile() {
        startContainerActivity(PersonalDataFragment.class.getCanonicalName());
    }
}
